//
//  ViewController.m
//  EmojiKeyboard
//
//  Created by Xiang on 2021/5/19.
//

#import "ViewController.h"
#import "WQEmojiKeyBoard.h"
#import "NSAttributedString+Emotion.h"

#define kRGBA(r, g, b, a) ([UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a])
#define kScreenW ([UIScreen mainScreen].bounds.size.width)
#define kScreenH ([UIScreen mainScreen].bounds.size.height)

@interface ViewController ()<WQEmojiKeyBoardDelegate>

@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) WQEmojiKeyBoard *emojiKeyBoard;

@property (nonatomic, strong) UIView *bottomToolView;

@property (nonatomic, strong) UIButton *swBtn;

@property (nonatomic) BOOL isShowEmojiKB;

@property (nonatomic) CGFloat keyBoardY;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kRGBA(246, 246, 246, 1);
    [self textView];
    [self emojiKeyBoard];
    [self swBtn];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}


- (UITextView *)textView{
    if (_textView == nil) {
        _textView = [[UITextView alloc]init];
        [self.view addSubview:_textView];
        _textView.frame = CGRectMake(10, 88, self.view.frame.size.width-20, 150);
        _textView.font = [UIFont systemFontOfSize:16];
        _textView.layer.borderWidth = 0.8f;
        _textView.layer.borderColor = kRGBA(230, 230, 230, 1).CGColor;
        _textView.layer.cornerRadius = 5;
        
        
        
        UIButton *btn = [[UIButton alloc]init];
        [self.view addSubview:btn];
        [btn setTitle:@"转换" forState:UIControlStateNormal];
        btn.frame = CGRectMake(10, _textView.frame.origin.y+_textView.frame.size.height+15, 60, 30);
        btn.backgroundColor = UIColor.redColor;
        [btn addTarget:self action:@selector(changBtnClick) forControlEvents:UIControlEventTouchUpInside];

        
    }
    return _textView;
}

- (WQEmojiKeyBoard *)emojiKeyBoard{
    if (_emojiKeyBoard == nil) {
        _emojiKeyBoard = [[WQEmojiKeyBoard alloc]initWithFrame:CGRectMake(0, self.bottomToolView.frame.origin.y, self.view.frame.size.width, 0) textView:self.textView];
        [self.view addSubview:_emojiKeyBoard];
        _emojiKeyBoard.delegate = self;
    }
    return _emojiKeyBoard;
}

- (UIView *)bottomToolView{
    if (_bottomToolView == nil) {
        _bottomToolView = [[UIView alloc]init];
        [self.view addSubview:_bottomToolView];
        _bottomToolView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 50);
        _bottomToolView.backgroundColor = kRGBA(230, 230, 230, 1);
    }
    return _bottomToolView;
}

- (UIButton *)swBtn{
    if (_swBtn == nil) {
        _swBtn = [[UIButton alloc]init];
        [self.bottomToolView addSubview:_swBtn];
        [_swBtn setTitle:@"切换为表情" forState:UIControlStateNormal];
        _swBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _swBtn.backgroundColor = UIColor.redColor;
        _swBtn.frame = CGRectMake(10, 10, 30, 30);
        [_swBtn sizeToFit];
        [_swBtn addTarget:self action:@selector(keyBoardSwitchBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _swBtn;
}



#pragma mark --- 底部view随键盘升起
- (void)keyboardWillChangeFrame:(NSNotification *)notification{
    NSLog(@"keyboardWillChangeFrame");
    NSDictionary *userInfo = notification.userInfo;
    double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
    
    if (keyboardF.origin.y >= kScreenH || keyboardF.origin.y == 0) {
        //键盘收起
        if (self.isShowEmojiKB == YES) {
            self.keyBoardY = self.bottomToolView.frame.origin.y+self.bottomToolView.frame.size.height;
        }else{
            [self invalidInputStatusWithAnimateDuration:duration keyboardY:keyboardF.origin.y];
            self.keyBoardY = keyboardF.origin.y;
        }
    }else{
        //键盘弹起
        [self effectInputStatusWithAnimateDuration:duration keyboardY:keyboardF.origin.y];
        self.keyBoardY = keyboardF.origin.y;
    }
    
}

- (void)effectInputStatusWithAnimateDuration:(double)duration keyboardY:(CGFloat)keyboardY{
    //弹起
    [UIView animateWithDuration:duration animations:^{
        self.bottomToolView.frame = CGRectMake(0, keyboardY-50, self.view.frame.size.width, 50);
        self.emojiKeyBoard.frame = CGRectMake(0, keyboardY, kScreenW, kScreenH-keyboardY);
        [self.emojiKeyBoard keyBoardFrameChaneWithFrame:self.emojiKeyBoard.frame];
    } completion:^(BOOL finished) {
        self.isShowEmojiKB = NO;
    }];
}


- (void)invalidInputStatusWithAnimateDuration:(double)duration keyboardY:(CGFloat)keyboardY{
    //收起
    [UIView animateWithDuration:duration animations:^{
        self.bottomToolView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 50);
        self.emojiKeyBoard.frame = CGRectMake(0, keyboardY, kScreenW, kScreenH-keyboardY);
        [self.emojiKeyBoard keyBoardFrameChaneWithFrame:self.emojiKeyBoard.frame];
    } completion:^(BOOL finished) {
    }];
}

- (void)keyBoardSwitchBtnClick{
    if (self.isShowEmojiKB == YES) {
        //切换为文件键盘
        self.isShowEmojiKB = NO;
        [self.swBtn setTitle:@"切换为表情" forState:UIControlStateNormal];
        [self.textView becomeFirstResponder];
    }else{
        //切换为表情键盘
        self.isShowEmojiKB = YES;
        [self.swBtn setTitle:@"切换为文字" forState:UIControlStateNormal];
        [self.textView resignFirstResponder];
    }
}


- (void)emojiKeyBoard:(WQEmojiKeyBoard *)emojiKeyBoard emojiBtnClickForIndex:(NSInteger)emojiBtnIndex value:(NSString *)value img:(NSString *)img{
    NSLog(@"emojiBtnIndex=%ld",emojiBtnIndex);
}

- (void)changBtnClick{
    NSString *tempStr = [self.textView.attributedText mgo_getPlainString];
    NSLog(@"转换为普通文本====%@",tempStr);
}


@end
